<template>
    <el-container class="home-container">
        <!--头部 -->
        <el-header>
            <div>
                <img class="logo" src="../assets/logo.jpg"/>
                <span>电商管理系统</span>
            </div>
            <el-button @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <!--侧边栏 -->
            <el-aside :width="isCollapse? '64px': '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409EFF" unique-opened
                :collapse="isCollapse" :collapse-transition="false" router :default-active="itemActive">
                    <!--一级菜单 -->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!--二级菜单 -->
                        <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children"
                        :key="subItem.id" @click="itemActiveChange('/' + subItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!--主体 -->
            <el-main>
                <!--路由占位符-->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    data () {
        return {
            // 左侧菜单数据
            menuList: [],
            // 字体图标对象
            iconObj: {
                125: 'iconfont icon-users',
                103: 'iconfont icon-tijikongjian',
                101: 'iconfont icon-shangpin',
                102: 'iconfont icon-danju',
                145: 'iconfont icon-baobiao'
            },
            isCollapse: false,
            // 选中的菜单栏
            itemActive: ''
        }
    },
    created () {
        this.getMenuList()
        this.itemActive = window.sessionStorage.getItem('activePath')
    },
    methods: {
        logout () {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有菜单
        async getMenuList () {
            const { data: res } = await this.$http.get('menus')
            // console.log(res)
            if (res.meta.status !== 200) return this.$message.error(res.meta.message)
            this.menuList = res.data
        },
        // 点击按钮切换菜单折叠与打开
        toggleCollapse () {
            this.isCollapse = !this.isCollapse
        },
        // 默认选中菜单栏
        itemActiveChange (activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.itemActive = activePath
        }
    }
}
</script>

<style lang="less" scoped>
    .iconfont {
        margin-right: 4px;
    }
    .home-container {
        height: 100%;
    }
    .el-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 0;
        font-size: 20px;
        color: #fff;
        background: linear-gradient(to left, rgba(1, 170, 237, 1), rgba(82, 183, 109, 1));
        div {
            display: flex;
            align-items: center;
        }
        .logo {
        width: 60px;
        height: 60px;
        border-radius: 30px;
        margin-right: 20px;
    }
    }
    .el-aside {
        background: rgba(57, 61, 73, 1);
        .el-menu {
            border-right: none;
        }
    }
    .el-main {
        background: #EFEEF0;
    }
    .toggle-button {
        background-color: #4A5064;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>
